<?php
    require_once(MODEL_PATH.'/MAPI/Base.php');
    
    class Model_MAPI_Version extends MODEL_MAPI_Base{
        
        
      function __construct(){
      }
        
  
      public function version_to_integer($version_string){
      	$util = getModel('Util');
      	list($major, $minor, $sub) = $util->safe_explode('.', $version_string, 3);
      	return $major * 10000 + $minor * 100 + $sub;
      }
      
      // $table_name : version control table 
      // $platform: platform_name
      protected function check_app_update_with_version($table_name, $platform_name, $app_version){  	
      	$platform_id =  $this->get_platform_id_by_name($platform_name); 
    
        if ($platform_id >=1 and $platform_id <=2){ 
      	  $dbh_r = $this->db_connect('read');  	
      	  $h_row = db_selectrow_hash($dbh_r, 'select version, force_update as force_update, message, url from '.$table_name.' where status=1 and platform=? order by id desc limit 1', $platform_id);
      	  
      	  
      	  if ($h_row){  	
      	  	$current_version_int = $this->version_to_integer($h_row['version']);
      	  	$app_version_int = $this->version_to_integer($app_version);
      	  	if ($app_version_int < $current_version_int){
      	      return $h_row;
      	    }
      	  } 
        }
        return (object)array();
      }
          
      protected function get_platform_id_by_name($name){
      	if ($name == 'iOS') return 1;  	
      	if ($name == 'ios') return 1;  	
      	if ($name == 'Android') return 2;
      	return 0;
      }
      
      
    }
    
?>